const db = require('../db/database');

class GroupDao {

    constructor(database){
        this.database = database;
    }

    createTable(){
        const sql = `
            create table if not exists sys_group (
                id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                name TEXT,
                desc TEXT
            )
        `;
        return this.database.run(sql);
    }

    insert(params){
        const sql = `
            insert into sys_group(name, desc)
            values(?, ?)
        `;
        return this.database.run(sql, [params.name, params.desc]);
    }

    update(params){
        const {id} = params;
        const sql = `
            update sys_group
            set name=?, desc=?
            where id=?
        `;
        return this.database.run(sql,[
            params.name,
            params.desc,
            id
        ]);
    }

    delete(id){
        const sql = `
            delete from sys_group where id=?
        `;
        return this.database.run(sql, [id]);
    }

    all(){
        return this.database.all('select * from sys_group');
    }

    getById(id){
        const sql = `
            select * from sys_group where id=?
        `;
        return this.database.get(sql, [id]);
    }

}

const groupDao = new GroupDao(db);

module.exports = groupDao;